package com.itheima.Jiuye17;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class XmlParse {
    public static void main(String[] args) throws DocumentException {
        //先获取解析器对象
        SAXReader saxReader = new SAXReader();
        //使用解析器将xml加载到内存，
        Document document = saxReader.read(new File("E:\\IEDA_Demo\\basic\\mJiuYe\\xml\\student.xml"));
        //并返回一个文档
        Element rootElement = document.getRootElement();
        //element（标签名）：可以获取调用者的所有的子标签内容，并把这些内容放在集合中返回
        List<Element> students = rootElement.elements("student");
        //创建一个集合对象
        ArrayList<Student> list = new ArrayList<>();
        //student依次表示每一个student标签
        for (Element student : students) {  //遍历这个集合获取所有student对象
            Attribute attribute = student.attribute("id");
            String s = attribute.getValue();      //获取到id的属性值
            Element element = student.element("name");  //获取name标签
            String s1 = element.getText();    //获取到name这个标签的标签体内容
            Element element1 = student.element("age");    //获取age标签
            String age = element1.getText();   //获取到age的标签体内容
            Student stu = new Student(s1,Integer.parseInt(age),s);
            list.add(stu);
        }
        for (Student student : list) {
            System.out.println(student);
        }
    }
}
